<?php

namespace App\Models;

use App\Models\SubAccount\SubAccount;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ShopRecord extends Model
{
    const UPDATED_AT = null;
    const CREATED_AT = null;
    use HasFactory;
    protected $guarded = [];
    protected $appends=['one_department_num','two_department_num','three_department_num'];
    //子账号数
//    protected function unReadNum(): Attribute
//    {
//        return new Attribute(
//            get: fn () => $this->getAccount(),
//        );
//    }
    protected function oneDepartmentNum(): Attribute
    {
        return new Attribute(
            get: fn () => $this->oneNum(),
        );
    }
    protected function twoDepartmentNum(): Attribute
    {
        return new Attribute(
            get: fn () => $this->twoNum(),
        );
    }
    protected function threeDepartmentNum(): Attribute
    {
        return new Attribute(
            get: fn () => $this->threeNum(),
        );
    }
    public function oneNum(){
        return ShopRecord::query()
            ->where('shop_manage_id',$this->attributes['shop_manage_id'])
            ->where('department_one_id','<>',0)
            ->where('department_two_id',0)
            ->where('department_three_id',0)
            ->where('is_read',1)
            ->count();
    }
    public function twoNum(){
        return ShopRecord::query()
            ->where('shop_manage_id',$this->attributes['shop_manage_id'])
            ->where('department_one_id','<>',0)
            ->where('department_two_id','<>',0)
            ->where('department_three_id',0)
            ->where('is_read',1)
            ->count();
    }
    public function threeNum(){
        return ShopRecord::query()
            ->where('shop_manage_id',$this->attributes['shop_manage_id'])
            ->where('department_one_id','<>',0)
            ->where('department_two_id','<>',0)
            ->where('department_three_id','<>',0)
            ->where('is_read',1)
            ->count();
    }

    public function first(){
        return $this->hasOne(Department::class,'id','department_one_id');
    }
    public function two(){
        return $this->hasOne(Department::class,'id','department_two_id');
    }
    public function three(){
        return $this->hasOne(Department::class,'id','department_three_id');
    }
    public function shop(){
        return $this->hasOne(ShopDetail::class,'id','shop_manage_id');
    }
    public function user()
    {
        return $this->hasOne(SystemUser::class,'id','user_id');
    }
}
